package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.domain.db.Log;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.LogVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.service.db.LogService;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserInfoManager {
    @Reference
    private AnalysisByDayService analysisByDayService;
    @Reference
    private UserInfoService userInfoService;
    @Reference
    private LogService logService;
    /**
     * 根据时间查找用户活动
     */
    public PageBeanVo findUserInfoByDate(Integer page, Integer pageSize, String date,String nickName) {
        PageBeanVo pageBeanVo = analysisByDayService.findUserInfoByDate(page, pageSize, date,nickName);
        ArrayList<LogVo> list = new ArrayList<>();
        //根据日期查
            List<Log> logList = (List<Log>) pageBeanVo.getItems();
            for (Log log : logList) {
                LogVo logVo = new LogVo();
                UserInfo userInfo = userInfoService.findById(log.getUserId());
                BeanUtil.copyProperties(userInfo,logVo);
                BeanUtil.copyProperties(log,logVo);
                list.add(logVo);
            }
        pageBeanVo.setItems(list);
      return pageBeanVo;
    }

    public PageBeanVo findUserInfoByUser(Integer page, Integer pageSize, String userName) {
        PageBeanVo pageBeanVo = analysisByDayService.findUserInfoByuserName(page, pageSize, userName);
        List<UserInfo> userInfoListList = (List<UserInfo>) pageBeanVo.getItems();
        ArrayList<LogVo> list = new ArrayList<>();
        for (UserInfo userInfo : userInfoListList) {
            List<Log> logList=logService.findLogById(userInfo.getId());
            for (Log log : logList) {
                LogVo logVo = new LogVo();
                BeanUtil.copyProperties(userInfo,logVo);
                BeanUtil.copyProperties(log,logVo);
                list.add(logVo);
            }
        }
        pageBeanVo.setItems(list);
        return pageBeanVo;
    }
}
